import {
    createRouter, createWebHashHistory,
    createWebHistory
} from "vue-router"
import homeRoute, {HOME_ROUTE} from '@/router/routes/home/home.js'
import loginRoute from '@/router/routes/login/login.js'
import categoryRoute from '@/router/routes/category/category.js'
import categoryProductListRoute from '@/router/routes/category/CategoryProductList.js'
import productRoute from '@/router/routes/product/product.js'
import moreRoute from '@/router/routes/more/more.js'
import carRoute from '@/router/routes/car/car.js'
import myRoute from '@/router/routes/my/my.js'
import settingRoute from '@/router/routes/setting/setting.js'
import updateAvatarRoute from '@/router/routes/setting/UpdateAvatar.js'
import updateAvatarBgRoute from '@/router/routes/setting/UpdateAvatarBg.js'
import updateUsernameRoute from '@/router/routes/setting/UpdateUsername.js'
import updatePwdRoute from '@/router/routes/setting/UpdatePwd.js'
import updateUserInfoRoute from '@/router/routes/setting/UpdateUserInfo.js'
import personalRoute from '@/router/routes/personal/personal.js'
import addressListRoute from '@/router/routes/address/AddressList.js'
import addAddressRoute from '@/router/routes/address/AddAddress.js'
import editorAddressRoute from '@/router/routes/address/EditorAddress.js'
import orderRoute from '@/router/routes/order/order.js'
import payRoute from '@/router/routes/order/pay.js'
import orderListRoute from '@/router/routes/order/OrderList.js'
import orderDetailRoute from '@/router/routes/order/OrderDetail.js'
import searchRoute from '@/router/routes/search/search.js'
import searchListRoute from '@/router/routes/search/SearchList.js'
import errorRoute from '@/router/routes/error/error.js'
import commentRoute from '@/router/routes/order/comment.js'
import myCommentRoute from '@/router/routes/my/MyComment.js'
import myCommentDetailRoute from '@/router/routes/my/MyCommentDetail.js'
import {afterEachHandler, beforeEachHandler} from "@/router/GlobalRouteGuard.js";
import liuYanRoute from "@/router/routes/liu/LiuYan.js";
import myLiuYanRoute from "@/router/routes/liu/MyLiuYan.js";
import writeLiuYanRoute from "@/router/routes/liu/WriteLiuYan.js";

const env = import.meta.env
const APP_BASE_URL = env.VITE_APP_BASEURL

// 根路由
export const ROOT_ROUTE = 'root'

/**
 * 创建并配置 vue-router 实例对象
 *
 * @type {Router} vue-router 实例对象
 */
const router = createRouter({
    // 页面历史模式采用 html5 历史模式（网页 url 中不带 #）
    // history: createWebHistory(),
    history: createWebHashHistory(),
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部
        return { top: 0 }
    },
    // 配置页面路由
    routes: [
        // 重定向到根路由
        {
            path: '/',
            redirect: {name: HOME_ROUTE}
        },
        // 根路由
        {
            name: ROOT_ROUTE,
            path: "/",
            children: [
                homeRoute,
                categoryRoute,
                categoryProductListRoute,
                productRoute,
                moreRoute,
                carRoute,
                loginRoute,
                myRoute,
                settingRoute,
                updateAvatarRoute,
                updateAvatarBgRoute,
                updateUsernameRoute,
                updatePwdRoute,
                updateUserInfoRoute,
                personalRoute,
                addressListRoute,
                addAddressRoute,
                editorAddressRoute,
                orderRoute,
                payRoute,
                orderListRoute,
                orderDetailRoute,
                errorRoute,
                searchRoute,
                searchListRoute,
                commentRoute,
                myCommentRoute,
                myCommentDetailRoute,
                liuYanRoute,
                myLiuYanRoute,
                writeLiuYanRoute,
            ]
        },
    ]
})

/**
 * 全局前置路由守卫
 */
router.beforeEach(beforeEachHandler)

/**
 * 全局后置路由守卫
 */
router.afterEach(afterEachHandler)

/**
 * 默认导出 vue-router 实例对象
 */
export default router